//
//  TwentyFourViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "TwentyFourViewController.h"
#import "TwentyFourMode.h"
#import "MVVMViewController.h"
@interface TwentyFourViewController ()<UITableViewDataSource>
/**  UITableView */
@property (nonatomic, strong) UITableView *tableView;
/**  数据源 */
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation TwentyFourViewController

-(void)xxxx:(UIBarButtonItem *)sender{
    MVVMViewController *mvvm = [[MVVMViewController alloc]init];
    [self.navigationController pushViewController:mvvm animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"mvc";
    [self registerNotification];
    [self initData];
    [self initUI];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(xxxx:)];
}
#pragma mark - 实例方法
- (void)registerNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getImageLoadTableView:) name:@"hasImage" object:nil];
}
- (void)initData{
    NSArray *array = @[@"http://pic2.sc.chinaz.com/files/pic/pic9/201605/apic20823.jpg",@"http://pics.sc.chinaz.com/files/pic/pic9/201604/apic20451.jpg"];
    self.dataSource = [NSMutableArray array];
    for (NSString *urlStr in array) {
        TwentyFourMode *model = [[TwentyFourMode alloc] init];
        [model loadImageWithUrlStr:urlStr];
    }
}
- (void)initUI{
    [self.view addSubview:self.tableView];
}
#pragma mark - 事件响应方法
- (void)getImageLoadTableView:(NSNotification *)notifi {
    [self.dataSource addObject:notifi.object];
//    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
//    });
}
#pragma mark - UITableViewDataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    TwentyFourMode *model = self.dataSource[indexPath.row];
    cell.imageView.image = model.image1;
    return cell;
}
#pragma mark - set方法

#pragma mark - get方法
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        CGRect rect = _tableView.frame;
        rect.origin.y += 20;
        _tableView.frame = rect;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end
